package cn.com.jenson.service.main.service.impl;

import cn.com.jenson.common.ImportVerifierVo;
import cn.com.jenson.common.SelectVo;
import cn.com.jenson.entity.main.QStory;
import cn.com.jenson.entity.main.Story;
import cn.com.jenson.exception.BusinessException;
import cn.com.jenson.service.main.repository.StoryRepository;
import cn.com.jenson.service.main.service.StoryService;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class StoryServiceImpl implements StoryService {

    QStory qStory = QStory.story;

    @Resource
    JPAQueryFactory jpaQueryFactory;

    @Resource
    StoryRepository storyRepository;

    @Override
    public Page<Story> pageAll(Story story) {
        JPAQuery<Story> jpaQuery = jpaQueryFactory
                .selectFrom(qStory)
                .where(getWhere(story));

        List<Story> list = jpaQuery
                .limit(story.getPageRequest().getPageSize())
                .offset(story.getPageRequest().getOffset())
                .fetch();

        supplement(list);

        return new PageImpl<>(
                list,
                story.getPageRequest(),
                jpaQuery.fetchCount()
        );
    }

    @Override
    public List<Story> listAll(Story story) {

        JPAQuery<Story> jpaQuery = jpaQueryFactory
                .selectFrom(qStory)
                .where(getWhere(story));

        List<Story> list = jpaQuery
                .limit(story.getPageRequest().getPageSize())
                .offset(story.getPageRequest().getOffset())
                .fetch();

        supplement(list);

        return list;
    }

    @Override
    public Story get(Story story) throws InstantiationException, IllegalAccessException {

        Story entity = storyRepository.findByIdAndDeleted(story.getId(),false).orElseThrow(
                ()->new BusinessException("暂未找到id为"+story.getId()+"的故事")
        );
        supplement(entity);
        return entity;
    }

    @Override
    public BooleanExpression getWhere(Story story) {
        return qStory.deleted.eq(false);
    }

    @Override
    public Story edit(Story story) throws InstantiationException, IllegalAccessException {
        return storyRepository.save(story);
    }

    @Override
    public List<Story> editAll(List<Story> stories) {
        return null;
    }

    @Override
    public Story deleted(Story story) throws InstantiationException, IllegalAccessException {
        Story entity = storyRepository.findByIdAndDeleted(story.getId(),false).orElseThrow(
                ()->new BusinessException("暂未找到id为"+story.getId()+"的故事")
        );
        entity.setDeleted(true);
        return  storyRepository.save(entity);
    }

    @Override
    public List<Story> deletedAll(List<Story> stories) {
        return null;
    }

    @Override
    public List<ImportVerifierVo> importTemplateVerifiers() {
        return null;
    }

    @Override
    public void importTemplateDownload(HttpServletResponse response) throws IOException {

    }

    @Override
    public List<SelectVo<String, Integer, Story>> option() {
        return null;
    }

    @Override
    public void supplement(Story story) {

    }

    @Override
    public void supplement(List<Story> stories) {

    }

    @Override
    public void supplement(Page<Story> stories) {

    }
}
